import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
import Welcome from '../views/Welcome.vue'
import Users from '../views/users/Users.vue'
import Roles from '../views/power/Roles.vue'
import Rights from '../views/power/Rights.vue'
import Categories from '../views/commodity/Categories.vue'
import Params from '../views/commodity/Params.vue'
import Goods from '../views/commodity/Goods.vue'
import Add from '../views/commodity/Add.vue'
import Orders from '../views/Orders.vue'
import Reports from '../views/Reports.vue'

Vue.use(VueRouter)

const routes = [
  // 主页路由
  { path: '/', redirect: '/login' },
  // 登录页路由
  { path: '/login', component: Login },
  // 主页路由
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/users', component: Users },
      { path: '/roles', component: Roles },
      { path: '/rights', component: Rights },
      { path: '/categories', component: Categories },
      { path: '/params', component: Params },
      { path: '/goods', component: Goods },
      { path: '/goods/add', component: Add },
      { path: '/orders', component: Orders },
      { path: '/reports', component: Reports }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  const token = sessionStorage.getItem('token')
  if (token) return next()
  if (to.path === '/login') return next()
  next('/login')
})

export default router
